package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.media.model.CourseTeacherDTO;
import com.xuecheng.content.convert.CourseTeacherConvert;
import com.xuecheng.content.entity.CourseTeacher;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.service.CourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * <p>
 * 课程-教师关系表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2019-11-05
 */
@Slf4j
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {

    public List<CourseTeacherDTO> courseTeacherList(Long courseBaseId, Long coursePubId) {
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(courseBaseId != null, CourseTeacher::getCourseId, courseBaseId);
        /*queryWrapper.eq(courseBaseId != null, CourseTeacher::getCoursePubId, coursePubId);*/
        List<CourseTeacher> teacherList = this.list(queryWrapper);

        return CourseTeacherConvert.INSTANCE.entitys2dtos(teacherList);
    }

    @Transactional
    public CourseTeacherDTO addCourseTeachers(CourseTeacherDTO courseTeacherDTO) {
        CourseTeacher courseTeacher = CourseTeacherConvert.INSTANCE.dto2entity(courseTeacherDTO);
        courseTeacher.setCreateDate(null);
        if (ObjectUtils.isEmpty(courseTeacherDTO.getCourseTeacherId())) {
            this.save(courseTeacher);
        }
        else {
            LambdaUpdateWrapper<CourseTeacher> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(CourseTeacher::getId,courseTeacher.getId());
            this.update(courseTeacher,wrapper);
        }
        return CourseTeacherConvert.INSTANCE.entity2dto(courseTeacher);
    }

    public boolean delCourseTeachers(Long courseBaseId, Long courseTeacherId) {
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(courseBaseId != null, CourseTeacher::getCourseId, courseBaseId);
        queryWrapper.eq(courseBaseId != null, CourseTeacher::getId, courseTeacherId);
        return this.remove(queryWrapper);
    }
}
